package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JOptionPane;

import databaseController.DatabaseController;

import model.Driver;
import model.DriverLicense;
import view.ServerViewPanelDriver;

public class ServerDriverController {

	private ServerViewPanelDriver panel;
	
	public ServerDriverController() {
		this.panel = new ServerViewPanelDriver();
		this.panel.getjButtonConfirm().addActionListener(new ListenerConfirm());
		this.panel.getjButtonClear().addActionListener(new ListenerClear());
	}
	
	public static Collection<Driver> getDrivers() {
		return null;
	}	
	
	public ServerViewPanelDriver getPanel() {
		return this.panel;
	}
	
	private void clear() {
		this.panel.getjTextFieldRegister().setText("");
		this.panel.getjTextFieldExpirationDate().setText("");
		this.panel.getjTextFieldUf().setText("");
		this.panel.getjTextFieldName().setText("");
		this.panel.getjTextFieldAddress().setText("");		
	}
	
	class ListenerConfirm implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Colocar registro de motorista na base de dados
			String register = ServerDriverController.this.panel.getjTextFieldRegister().getText();
			String expirationDate = ServerDriverController.this.panel.getjTextFieldExpirationDate().getText();
			String uf = ServerDriverController.this.panel.getjTextFieldUf().getText();
			String name = ServerDriverController.this.panel.getjTextFieldName().getText();
			String address = ServerDriverController.this.panel.getjTextFieldAddress().getText();
			
			DatabaseController.getInstance().addDriver(new Driver(new DriverLicense(register, expirationDate, uf), name, address));
			
			JOptionPane.showMessageDialog(null,
				    "Motorista cadastrado com sucesso!\n " , 
				    "Sucesso", JOptionPane.INFORMATION_MESSAGE);
			
			ServerDriverController.this.clear();			
			
			System.out.println("*=*   Driver\'s Register   *=*");
			System.out.println("Register: " + register);
			System.out.println("Expiration Date: " + expirationDate);
			System.out.println("Uf: " + uf);
			System.out.println("Name: " + name);
			System.out.println("Address: " + address);		
			System.out.println("*=*   ------------------   *=*");
		}
	}
	
	class ListenerClear implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerDriverController.this.clear();
		}
	}	
}
